/*
 * This file is Copyright 2002-2006 Loyola University Chicago,
 * Department of Computer Science and the Emerging Technologies Laboratory.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License via
 *    http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.citep.web.admin.users.client;

import com.citep.web.admin.accounts.client.AccountService;
import com.citep.web.admin.accounts.client.AccountServiceAsync;
import com.citep.web.admin.accounts.client.AccountShortcut;
import com.citep.web.gwt.dto.AccountCategoryDTO;
import com.citep.web.gwt.dto.RoleDTO;
import com.citep.web.gwt.dto.UserDTO;
import com.citep.web.gwt.module.BaseApplication;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import java.util.ArrayList;
import java.util.HashMap;


public class UserEntryApplication  extends BaseApplication {
	private UserEntryPresentation presentation;
	
	private AsyncCallback updateRoleListCallback;
	private AsyncCallback addUserCallback;
	private AsyncCallback getUserCallback;
	private AsyncCallback editUserCallback;

	private AccountServiceAsync accountsService;
	private HashMap roleMap = new HashMap(); //maps role names to role objects

	private UserDTO currentUser = new UserDTO(); //user we are currently editing
	
	public UserEntryApplication() {
		accountsService = (AccountServiceAsync)GWT.create(AccountService.class);
		((ServiceDefTarget)accountsService).setServiceEntryPoint(GWT.getModuleBaseURL() + "/accounts");
	}
	
	protected AsyncCallback getUpdateRoleListCallBack() {
		if(updateRoleListCallback == null) {
			updateRoleListCallback = new AsyncCallback() {
				public void onSuccess(Object result) {
					ArrayList roles = (ArrayList)result;
					for(int i = 0; i < roles.size(); i++) {
						RoleDTO role = (RoleDTO)roles.get(i);
						presentation.addRole(role.getName());
						roleMap.put(role.getName(), role);
					}
				}
				
				public void onFailure(Throwable caught) {
					processException(presentation,caught);
				}
			};
		}
		
		return updateRoleListCallback;
	}

	protected AsyncCallback getAddUserCallCallBack() {
		if(addUserCallback == null) {
			addUserCallback = new AsyncCallback() {
				public void onSuccess(Object result) {
					presentation.displayStatus("User saved", 3);
					presentation.setSaveState();					
				}
				
				public void onFailure(Throwable caught) {
					processException(presentation,caught);
				}
			};
		}
		
		return addUserCallback;
	}

	protected AsyncCallback getGetUserCallBack() {
		if(getUserCallback == null) {
			getUserCallback = new AsyncCallback() {
				public void onSuccess(Object result) {
					UserDTO retrievedUser = (UserDTO)result;					
					presentation.setEditState();
					presentation.setUserData(retrievedUser);
					currentUser = retrievedUser;
					
				}
				
				public void onFailure(Throwable caught) {
					processException(presentation,caught);
				}
			};
		}
		
		return getUserCallback;
	}

	protected AsyncCallback getEditUserCallBack() {
		if(addUserCallback == null) {
			addUserCallback = new AsyncCallback() {
				public void onSuccess(Object result) {
					presentation.displayStatus("User updated", 3);
					presentation.setEditState();					
				}
				
				public void onFailure(Throwable caught) {
					processException(presentation,caught);
				}
			};
		}
		
		return addUserCallback;
	}
	
	public void setPresentation(UserEntryPresentation presentation) {
		this.presentation = presentation;
	}

	public void updateRoleList() {
		presentation.clearRoleList();
		accountsService.getRoles(getUpdateRoleListCallBack());
	}
	
	public void addUser(String firstName, String lastName, String email, String password, String roleName) {
		RoleDTO role = (RoleDTO)roleMap.get(roleName);
		accountsService.createUser(firstName, lastName, email, password, role.getId(), getAddUserCallCallBack());
	}
	
	public void getUser(int userId) {
		accountsService.getUser(userId, getGetUserCallBack());
	}
	
	
	public void saveCurrentUser() {
		if(currentUser != null) {
			presentation.getUserData(currentUser);
			accountsService.saveUser(currentUser, getEditUserCallBack());
		}
	}
}